package cn.gok.homework;
import java.util.Random;
import java.util.Scanner;
public class homework1 {
    //    1 输入密码长度，至少6位
    //    2 生成随机密码字符串并输出，要求同时包含大写字母及数字
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入生成的密码长度:");
        int len = sc.nextInt();
        String randomPassWord = getRandomPassWord(len);
        System.out.println(randomPassWord);
    }
    //获取随机密码
    public static String getRandomPassWord(int length){
        String result = null;
        //要求密码最少6位
        while (length>6){
            result = randomPassWord(length);
            if (result.matches(".*[A-Z]{1,}.*")&&result.matches(".*\\d{1,}.*")){
                return result;
            }
            result = randomPassWord(length);
        }
        return "密码的长度不能小于6位";
    }
    public static String randomPassWord(int length){
        char ch[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".toCharArray();
        StringBuilder stringBuilder = new StringBuilder();
        Random random = new Random();
        for (int i = 0;i<length;i++){
            stringBuilder.append(ch[random.nextInt(ch.length)]);
        }
        return stringBuilder.toString();
    }
}